package com.sky.service.impl;

import com.sky.dto.GoodsSalesDTO;
import com.sky.entity.Orders;
import com.sky.mapper.OrderMapper;
import com.sky.mapper.UserMapper;
import com.sky.service.OrderService;
import com.sky.service.ReportService;
import com.sky.vo.*;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
@Slf4j
public class ReportServiceImpl implements ReportService {
    @Autowired
    private OrderMapper orderMapper;
    @Autowired
    private UserMapper userMapper;

    @Override
    public TurnoverReportVO getTurnoverStatistics(LocalDate begin, LocalDate end) {
        //获得所有日子
        List<LocalDate> dateList = getAllDate(begin, end);

        //统计每天的营业额
        List<Double> turnoverList = new ArrayList<>();
        for (LocalDate date : dateList) {
            //获得一天的开始结束
            LocalDateTime beginTime = LocalDateTime.of(date,LocalDateTime.MIN.toLocalTime());
            LocalDateTime endTime = LocalDateTime.of(date,LocalDateTime.MAX.toLocalTime());

            //通过sql语句统计这天订单的总和  select sum(amount) from orders where order_time > ? and <? and status = 5
            Integer status = Orders.COMPLETED;
            Map map = getMap(beginTime, endTime, status);
            Double turnover = orderMapper.sumByMap(map);
            turnover = turnover == null ? 0.0 : turnover;
            turnoverList.add(turnover);
        }
        return TurnoverReportVO.builder()
                .dateList(StringUtils.join(dateList,","))
                .turnoverList(StringUtils.join(turnoverList,","))
                .build();
    }



    @Override
    public UserReportVO getUserStatistics(LocalDate begin, LocalDate end) {
        //获得所有日子
        List<LocalDate> dateList = getAllDate(begin, end);


        List<Integer> totalUserList = new ArrayList<>();
        List<Integer> newUserList = new ArrayList<>();
        //遍历添加每日总用户和新用户
        for (LocalDate date : dateList) {
            //获得一天的开始结束
            LocalDateTime beginTime = LocalDateTime.of(date,LocalDateTime.MIN.toLocalTime());
            LocalDateTime endTime = LocalDateTime.of(date,LocalDateTime.MAX.toLocalTime());

            //select count(id) from users where creat_time <end.....
            Map map = new HashMap();
            map.put("end",endTime);

            Integer totalUser = userMapper.countByMap(map);
            totalUser = totalUser == null ? 0 : totalUser;
            totalUserList.add(totalUser);

            map.put("begin",beginTime);
            Integer newlUser = userMapper.countByMap(map);
            newlUser = newlUser == null ? 0 : newlUser;
            newUserList.add(newlUser);
        }
        return UserReportVO.builder()
                .dateList(StringUtils.join(dateList,","))
                .totalUserList(StringUtils.join(totalUserList,","))
                .newUserList(StringUtils.join(newUserList,","))
                .build();
    }

    @Override
    public OrderReportVO getOrderReportVO(LocalDate begin, LocalDate end) {
        //获得所有天
        List<LocalDate> dateList = getAllDate(begin, end);

        //获得每日总订单数和每日有效订单数
        List<Integer> orderCountList = new ArrayList<>();
        List<Integer> validOrderCountList = new ArrayList<>();
        Integer totalOrderCount = 0;
        Integer validOrderCount = 0;
        for (LocalDate date : dateList) {
            //获得一天的开始结束
            LocalDateTime beginTime = LocalDateTime.of(date,LocalDateTime.MIN.toLocalTime());
            LocalDateTime endTime = LocalDateTime.of(date,LocalDateTime.MAX.toLocalTime());

            //获得每日所有订单,顺便计算订单总数
            Map map = getMap(beginTime, endTime, null);
            Integer totalOrders = orderMapper.countByMap(map);
            totalOrders = totalOrders == null ? 0 : totalOrders;
            totalOrderCount += totalOrders;
            orderCountList.add(totalOrders);

            //获得每日有效订单
            map.put("status",Orders.COMPLETED);
            Integer validOrders = orderMapper.countByMap(map);
            validOrders = validOrders == null ? 0 : validOrders;
            validOrderCount += validOrders;
            validOrderCountList.add(validOrders);
        }

        //计算订单完成率
        Double orderCompletionRate =   validOrderCount/Double.valueOf(totalOrderCount) ;

        return OrderReportVO.builder()
                .dateList(StringUtils.join(dateList,","))
                .orderCountList(StringUtils.join(orderCountList,","))
                .validOrderCountList(StringUtils.join(validOrderCountList,","))
                .totalOrderCount(totalOrderCount)
                .validOrderCount(validOrderCount)
                .orderCompletionRate(orderCompletionRate)
                .build();
    }

    @Override
    public SalesTop10ReportVO getTop10(LocalDate begin, LocalDate end) {
        LocalDateTime beginTime = LocalDateTime.of(begin,LocalDateTime.MIN.toLocalTime());
        LocalDateTime endTime = LocalDateTime.of(end,LocalDateTime.MAX.toLocalTime());

        List<GoodsSalesDTO> goodsSalesDTOList = orderMapper.getTop10(beginTime,endTime);
        List<String> names = goodsSalesDTOList.stream().map(GoodsSalesDTO::getName).collect(Collectors.toList());
        String nameList = StringUtils.join(names, ",");

        List<Integer> numbers = goodsSalesDTOList.stream().map(GoodsSalesDTO::getNumber).collect(Collectors.toList());
        String numberList = StringUtils.join(numbers,",");

        return SalesTop10ReportVO
                .builder()
                .nameList(nameList)
                .numberList(numberList)
                .build();
    }

    private static List<LocalDate> getAllDate(LocalDate begin, LocalDate end) {
        List<LocalDate> dateList = new ArrayList<>();
        dateList.add(begin);

        while (!begin.equals(end)) {
            begin = begin.plusDays(1);
            dateList.add(begin);
        }
        return dateList;
    }

    private static Map getMap(LocalDateTime beginTime, LocalDateTime endTime, Integer status) {
        Map map = new HashMap();
        map.put("begin", beginTime);
        map.put("end", endTime);
        map.put("status", status);
        return map;
    }
}
